<?php
namespace App\Http\Controllers\Open;
use App\Http\Controllers\Common\CommonController;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use App\Models\Company\CompanyContact;
use App\Models\Company\CompanyDepartment;
use App\Models\Company\CompanyJob;
use App\Repositories\Eloquent\YiwangAccessToken;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class JobController extends CommonController{

    public function __construct(Request $request)
    {
        $yiwangAccessToken = new YiwangAccessToken();
        $yiwangAccessToken->checkAccessToken($request->access_token);
    }

    public function index(){

        $company_job =DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select(
                't1.id','t1.com_id','t1.job_title','t1.industry',
                't1.job_category1','t1.job_category2','t1.job_category3','t1.job_category4','t1.job_category5',
                't1.location1','t1.location2','t1.location3','t1.location4','t1.location5',
                't1.recruiting_num','t1.salary_above','t1.salary_below','t1.description','t1.welfare',
                't1.education','t1.work_year','t1.age_above','t1.age_below','t1.gender','t1.updatetime',
                't2.company_name','t2.region','t2.industry','t2.address','t2.employee_num','t1.urgent',
                't2.longitude','t2.latitude'
            )
            ->orderBy('updatetime','desc')
            ->paginate(10);
        $list=array();
        foreach($company_job as $k=>$v){
            //工作地区
            $v->location1 = CollArea::getAreaName($v->location1);
            $v->location2 = CollArea::getAreaName($v->location2);
            $v->location3 = CollArea::getAreaName($v->location3);
            $v->location4 = CollArea::getAreaName($v->location4);
            $v->location5 = CollArea::getAreaName($v->location5);

            $v->location =  $v->location1?$v->location1.';':'';
            $v->location .=  $v->location2?$v->location2.';':'';
            $v->location .=  $v->location3?$v->location3.';':'';
            $v->location .=  $v->location4?$v->location4.';':'';
            $v->location .=  $v->location5?$v->location5.';':'';


            $v->category1 = CollPosition::getPosName($v->job_category1);
            $v->category2 = CollPosition::getPosName($v->job_category2);
            $v->category3 = CollPosition::getPosName($v->job_category3);
            $v->category4 = CollPosition::getPosName($v->job_category4);
            $v->category5 = CollPosition::getPosName($v->job_category5);

            $v->category =  $v->category1?$v->category1.';':'';
            $v->category .=  $v->category2?$v->category2.';':'';
            $v->category .=  $v->category3?$v->category3.';':'';
            $v->category .=  $v->category4?$v->category4.';':'';
            $v->category .=  $v->category5?$v->category5.';':'';

            switch($v->gender){
                case 0:
                    $v->gender='不限';
                    break;
                case 1:
                    $v->gender='男';
                    break;
                case 2:
                    $v->gender='女';
                    break;
            }

            if(empty($v->age_above) and empty($v->age_below)){
                $age='不限';
            }elseif(empty($v->age_below)){
                $age=$v->age_above.'岁以上';
            }elseif(empty($v->age_above)){
                $age=$v->age_below.'岁以下';
            }else{
                $age=$v->age_above.'岁-'.$v->age_below.'岁';
            }

            switch($v->gender){
                case 0:
                    $sex='不限';
                    break;
                case 1:
                    $sex='男';
                    break;
                case 2:
                    $sex='女';
                    break;
            }
            $list[]=array(
                'posid'=>$v->id,//职位维一id
                'comid'=>$v->com_id,//公司维一id
                'posname'=>$v->job_title,//职位名称
                'company'=>$v->company_name,//公司名称
                'poscate'=>$v->category,//职位分类
                'hiring'=>$v->recruiting_num?$v->recruiting_num.'名':'若干名',//招聘人数
                'minsalary'=>$v->salary_above,//小最月薪 int
                'salary'=>$v->salary_above?$v->salary_above.'-'.$v->salary_below:'面议',//月薪
                'workareas'=>$v->location,//工作地区
                'industry'=>CollOption::getOptionName($v->industry,'industry'),//所属行业
                'address'=>$v->address,//地址
                'educational'=>CollOption::getOptionName($v->education,'education')?:'不限',//学历要求
                'age'=>$age,//年龄要求
                'experience'=>CollOption::getOptionName($v->work_year,'work_year')?:'不限',//工作经验要求
                'sex'=>$sex,//性别要求
                'welfare'=>$v->welfare,//福利
                'descriptions'=>$v->description,//描述
                'source'=>url('jobinfo_'.idEncryption($v->id).'.html'),//职位展示网址
                'comsource'=>url('comabout_'.idEncryption($v->com_id).'.html'),//公司展示网址
                'updatetime'=>$v->updatetime//用户真实更新时间,datatime类型
            );
        }
        return response()->json($list);
    }

    /**
     * 工作详细内容
     * @param $jobid
     * @return mixed
     */
    public function jobInfo(Request $request)
    {
        $jobid = $request->id;
        $jobInfo = CompanyJob::with(['company_basic'])->where('id', '=', $jobid)->first();//当前工作页面详情

        //工作地区
        $jobInfo->location1 = CollArea::getAreaName($jobInfo->location1);
        $jobInfo->location2 = CollArea::getAreaName($jobInfo->location2);
        $jobInfo->location3 = CollArea::getAreaName($jobInfo->location3);
        $jobInfo->location4 = CollArea::getAreaName($jobInfo->location4);
        $jobInfo->location5 = CollArea::getAreaName($jobInfo->location5);

        $jobInfo->location =  $jobInfo->location1?$jobInfo->location1.';':'';
        $jobInfo->location .=  $jobInfo->location2?$jobInfo->location2.';':'';
        $jobInfo->location .=  $jobInfo->location3?$jobInfo->location3.';':'';
        $jobInfo->location .=  $jobInfo->location4?$jobInfo->location4.';':'';
        $jobInfo->location .=  $jobInfo->location5?$jobInfo->location5.';':'';


        $jobInfo->category1 = CollPosition::getPosName($jobInfo->job_category1);
        $jobInfo->category2 = CollPosition::getPosName($jobInfo->job_category2);
        $jobInfo->category3 = CollPosition::getPosName($jobInfo->job_category3);
        $jobInfo->category4 = CollPosition::getPosName($jobInfo->job_category4);
        $jobInfo->category5 = CollPosition::getPosName($jobInfo->job_category5);

        $jobInfo->category =  $jobInfo->category1?$jobInfo->category1.';':'';
        $jobInfo->category .=  $jobInfo->category2?$jobInfo->category2.';':'';
        $jobInfo->category .=  $jobInfo->category3?$jobInfo->category3.';':'';
        $jobInfo->category .=  $jobInfo->category4?$jobInfo->category4.';':'';
        $jobInfo->category .=  $jobInfo->category5?$jobInfo->category5.';':'';

        $jobInfo->education=CollOption::getOptionName($jobInfo->education,'education');
        $jobInfo->work_year=CollOption::getOptionName($jobInfo->work_year,'work_year');


        switch($jobInfo->contact_info){
            case 0:
                $jobInfo->contacts=$jobInfo->company_basic->contacts;
                $jobInfo->mobile=$jobInfo->company_basic->mobile;
                $jobInfo->phone=$jobInfo->company_basic->phone;
                break;
            case 1:
                if(!empty($jobInfo->department)){
                    $companyDepartment=CompanyDepartment::where('id',$jobInfo->department)->first();
                    if(!empty($companyDepartment->id)){
                        $jobInfo->contacts=$companyDepartment->contacts;
                        $jobInfo->mobile=$companyDepartment->mobile;
                        $jobInfo->phone=$companyDepartment->phone;
                    }
                }
                break;
            case 2:
                $companyContact=CompanyContact::where('job_id',$jobInfo->id)->first();
                if(!empty($companyContact->id)){
                    $jobInfo->contacts=$companyContact->contacts;
                    $jobInfo->mobile=$companyContact->mobile;
                    $jobInfo->phone=$companyContact->phone;
                }
                break;
        }


        switch($jobInfo->gender){
            case 0:
                $jobInfo->gender='不限';
                break;
            case 1:
                $jobInfo->gender='男';
                break;
            case 2:
                $jobInfo->gender='女';
                break;
        }

        $jobInfoJson=array(
            "jobinfo"=>array(
                'posname'=>$jobInfo->job_title,//职位名称
                'hiring'=>$jobInfo->recruiting_num?$jobInfo->recruiting_num.'名':'若干名',//招聘人数
                'salary'=>$jobInfo->salary_above?$jobInfo->salary_above.'-'.$jobInfo->salary_below:'面议',//月薪
                'workareas'=>$jobInfo->location,//工作地区
                "address"=>$jobInfo->company_basic->address,//地址
                'educational'=>$jobInfo->education?$jobInfo->education:'不限',//学历要求
                'age'=>$jobInfo->age_above?$jobInfo->age_above.'岁-'.$jobInfo->age_below.'岁':'不限',//年龄要求
                'experience'=>$jobInfo->work_year?$jobInfo->work_year:'不限',//工作经验要求
                'sex'=>$jobInfo->gender,//性别要求
                'updated_at'=>$jobInfo->updatetime
            ),
            "cominfo"=>array(
                "logo"=>$jobInfo->company_basic->logo?url('/upload/companylogo/'.$jobInfo->company_basic->logo):'',//logo
                "company_name"=>$jobInfo->company_basic->company_name,//公司名称
                "brands"=>$jobInfo->company_basic->brands,//公司品牌
                "industry"=>CollOption::getOptionName($jobInfo->company_basic->industry,'industry'),//行业
                "employee_num"=>$jobInfo->company_basic->employee_num,//公司人数
                "profile"=>$jobInfo->company_basic->profile,//公司详细简绍
                "longitude"=>$jobInfo->company_basic->longitude,//经度
                "latitude"=>$jobInfo->company_basic->latitude,//纬度
                "coordinate_address"=>$jobInfo->company_basic->coordinate_address,//地图地址
            ),
            /*"contact"=>array(
                "contacts"=>$jobInfo->contacts?:'未公开',//联系人
                "mobile"=>$jobInfo->mobile?:'未公开',//手机
                "phone"=>$jobInfo->phone?:'未公开'//电话
            ),*/
            "description"=>array(
                "description"=>$jobInfo->description//职位描述
            )
        );

        if($request->contact==1){
            $jobInfoJson['contact']=array(
                "contacts"=>$jobInfo->contacts?:'未公开',//联系人
                "mobile"=>$jobInfo->mobile?:'未公开',//手机
                "phone"=>$jobInfo->phone?:'未公开'//电话
            );
        }

        return response()->json($jobInfoJson);
    }

}

